/*
 * Copyright (C) 2010 恒康信息科技有限公司
 * 版权所有
 */
package com.jjmmbb.kebiao.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 数据库基本操作类,数据库的创建,更新的操作都在这里进行
 * 
 * @author yizhe
 * @date 2012-5-18
 */
public abstract class DBHelper extends SQLiteOpenHelper {

	static String name = "kebiao.db"; // 数据库名称
	static CursorFactory cursorFactory = null;
	Context context;

	String tableName = ""; // 表名,留给子类设置
	ContentValues contentValues; // cursor对象转行中介

	static int version = 300;

	public DBHelper(Context context) {
		super(context, name, cursorFactory, version);
		this.context = context;
		this.tableName = setTableName();
	}

	// 强制实现,防止漏写表名
	abstract String setTableName();

	/**
	 * 软件第一次安装的时候会调用,覆盖安装不会调用
	 */
	public void onCreate(SQLiteDatabase db) {
		// 所有表的创建过程都在这里进行
		DBCreate.CreateDatabase(db);
	}

	/**
	 * 覆盖安装,当版本号version发生变化的时候,这个方法才会被调用,而且只执行一次
	 */
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		onCreate(db);
	}

	/**
	 * 每次成功打开数据库后首先被执行
	 */
	public void onOpen(SQLiteDatabase db) {
		super.onOpen(db); // 每次成功打开数据库后首先被执行
	}

	// --------------------------sql方法---------------------------------//
	/**
	 * 执行sql,没有返回
	 */
	public void execSQL(String sql) {
		System.out.println("==execSQL== " + sql);
		SQLiteDatabase db = getWritableDatabase();
		db.execSQL(sql);
		db.close();
	}

	/**
	 * 批量执行sql,没有返回
	 */
	public void execSQLBatch(ArrayList<String> list) {
		SQLiteDatabase db = getWritableDatabase();
		db.beginTransaction();
		try {
			for (String sql : list) {
				db.execSQL(sql);
			}
			db.setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endTransaction();
			db.close();
		}
	}

	public void finalize() {
		close();
	}

	/**
	 * 根据id删除记录
	 * 
	 * @param id
	 *            表中必须有"id"字段
	 * @return the number of rows affected if a whereClause is passed in, 0
	 *         otherwise. To remove all rows and get a count pass "1" as the
	 *         whereClause.
	 */
	public int delete(int id) {
		SQLiteDatabase db = getWritableDatabase();
		int result = db.delete(tableName, "id=?", new String[] { id + "" });
		db.close();
		return result;
	}

	public int delete(String whereStr, String[] arr) {
		SQLiteDatabase db = getWritableDatabase();
		int result = db.delete(tableName, whereStr, arr);
		db.close();
		return result;
	}

	/**
	 * 清空表
	 */
	public void clearTable() {
		SQLiteDatabase db = getWritableDatabase();
		db.execSQL("delete from " + tableName);
		db.close();
	}

	/**
	 * 按sql查询数据
	 */
	public Cursor Query(SQLiteDatabase db, String sql) {
		System.out.println("==Query==" + sql);
		return db.rawQuery(sql, new String[] {});
	}

	/**
	 * 返回查询的第一个单元格
	 */
	public int QueryIntegerObject(String sql) {
		int result = 0;
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, new String[] {});
		cursor.moveToFirst();
		if (!(cursor.isAfterLast())) {
			result = cursor.getInt(0);
		}
		cursor.close();
		db.close();

		return result;
	}

	/**
	 * 返回查询的第一个单元格
	 */
	public long QueryLongObject(String sql) {
		long result = 0;
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, new String[] {});
		cursor.moveToFirst();
		if (!(cursor.isAfterLast())) {
			result = cursor.getLong(0);
		}
		cursor.close();
		db.close();

		return result;
	}

	/**
	 * 按sql查询数据
	 */
	public String QueryStringObject(String sql) {
		String result = "";
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, new String[] {});
		cursor.moveToFirst();
		if (!(cursor.isAfterLast())) {
			result = cursor.getString(0);
		}
		cursor.close();
		db.close();

		return result;
	}

}
